package cn.pigeon.health.service.impl;

import cn.pigeon.health.constant.MessageConstant;
import cn.pigeon.health.dao.MemberDao;
import cn.pigeon.health.dao.OrderDao;
import cn.pigeon.health.dao.OrderSettingDao;
import cn.pigeon.health.entity.Result;
import cn.pigeon.health.pojo.Member;
import cn.pigeon.health.pojo.Order;
import cn.pigeon.health.pojo.OrderSetting;
import cn.pigeon.health.service.OrderService;
import cn.pigeon.health.service.OrderSettingService;
import cn.pigeon.health.utils.DateUtils_Extend;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预约单服务层实现类
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;

    /**
     * 按条件查询
     * @param map
     * @return
     */
    public Order findByCondition(Map map){
        return orderDao.findByCondition(map);
    }

    /**
     * 添加预约单
     * @param map
     */
    public Result makeOrder(Map<String,Object> map) throws Exception{
        //订单类型
        String orderType = (String) map.get("orderType");
        //姓名
        String name = (String) map.get("name");
        //性别
        Integer sex = Integer.parseInt(map.get("sex").toString());
        //身份证号
        String idCard = (String) map.get("idCard");
        //体检日期
        String orderDate = (String) map.get("orderDate");
        //手机号
        String telephone = (String) map.get("telephone");
        //套餐Id
        Integer setmealId = Integer.parseInt(map.get("setmealId").toString());

        //1.判断用户选择的体检日期，在预约管理t_ordersetting表中是否设置了该日期，没有设置，则不允许预约
        OrderSetting orderSetting = orderSettingDao.findByDate(orderDate);
        if(orderSetting==null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //2.判断当前日期是否已预约满了
        if(orderSetting.getReservations()>=orderSetting.getNumber()){
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        //3.判断用户是否重复预约了
        //3.1先根据手机号查询该用户判断是否已注册了
        Map<String,Object> mapCondition = new HashMap<String,Object>();
        mapCondition.put("phoneNumber",telephone);
        Member member = memberDao.findByCondition(mapCondition);
        if(member!=null){
            //判断用户是否重复注册了
            mapCondition = new HashMap<String,Object>();
            mapCondition.put("memberId",member.getId());
            mapCondition.put("orderDate",orderDate);
            mapCondition.put("setmealId",setmealId);
            Order order = orderDao.findByCondition(mapCondition);
            if(order!=null){
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }else{
            //注册添加新会员
            member = new Member();
            member.setName(name);
            member.setIdCard(idCard);
            member.setPhoneNumber(telephone);
            member.setSex(sex.toString());
            memberDao.add(member);
        }

        //3.2添加预约单
        Order newOrder = new Order(member.getId(), DateUtils_Extend.parseString2Date(orderDate,"yyyy-MM-dd")
                ,orderType,Order.ORDERSTATUS_NO,setmealId);
        orderDao.add(newOrder);

        //3.3修改预约人数
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.EditReservationsByDate(orderSetting);

        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS,newOrder.getId());
    }

    /**
     * 根据预约ID查询预约详情
     * @param id
     * @return
     */
    public Map findById(Integer id) {
        return orderDao.findById(id);
    }

    /**
     * 套餐预约数量统计
     * @return
     */
    public List<Map<String, Object>> findSetmealCount() {
        return orderDao.findSetmealCount();
    }
}
